package material

import (
	"cloud_android_backend/pkg/models/material"
	"github.com/golang/glog"
	"github.com/jinzhu/gorm"
	"modules.tenxcloud.com/common/composite"
)

func GetCategoriesList(userId string) ([]material.MaterialCategories, error) {

	var categorieList []material.MaterialCategories
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Errorf("get database connection failed, %s", err)
		return categorieList, err
	}
	categoriesModel := material.MaterialCategories{UserId: userId}
	count, err := categoriesModel.CountByUserId(db)
	if err != nil {
		glog.Errorf("get categories count failed, %s", err)
		if err != gorm.ErrRecordNotFound {
			glog.Errorf("get categories count failed, %s", err)
			return categorieList, err
		}
	}
	if count == 0 {
		categoriesModel.Category = "default"
		categoriesModel.Status = 1
		categoriesModel.UserId = userId
		err = categoriesModel.Create(db)
		if err != nil {
			glog.Errorf("create categories failed, %s", err)
			//return categorieList, err
		}
	}
	list, err := categoriesModel.GetAll(db)
	if err != nil {
		glog.Errorf("get categories failed, %s", err)
		return categorieList, err
	}
	categorieList = list
	return categorieList, err
}

func CreateCategory(userId string, category string, img string) (material.MaterialCategories, error) {
	var categoriesModel material.MaterialCategories
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Errorf("get database connection failed, %s", err)
		return categoriesModel, err
	}
	categoriesModel.Category = category
	categoriesModel.Status = 1
	categoriesModel.UserId = userId
	categoriesModel.Img = img
	err = categoriesModel.Create(db)
	return categoriesModel, err
}

func UploadFile(catId int, path string, userId string) {

}
